package yl.hs.bmipfull.controllers.poundroom;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.material.*;
import yl.hs.bmipfull.service.system.IOrganizationInfoService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作用:   <br/>
 * 创建时间: 2021-07-02 09:34 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "磅房Web接口", tags = "过磅管理——原材入库过磅管理")
@RequestMapping(value = "/api/RawmaterialsWarehousing")
@RestController
public class RawMaterialReceiptController extends ControllerBase {
    @Autowired
    ISupplierService supplierService;
    @Autowired
    IRMWarehouseService irmWarehouseService;
    @Autowired
    IRawMaterialCateService RawMaterialCateservice;
    @Autowired
    IRawMaterialInfoService RawMaterialInfoservice;
    @Autowired
    IPurchaseContractService purchaseContractServiceservice;
    @Autowired
    IPurchaseContractProductService purchaseContractProductService;
    @Autowired
    IPurchaseReceiptService purchaseReceiptServiceservice;

    @Autowired
    IOrganizationInfoService orgSvc;

    @ApiOperation(value = "保存原材入库单", notes = "查询/更新原材入库单信息")
    @RequestMapping(value = "/rawMaterialReceipt", method = RequestMethod.PUT)
    public WebResult saveRawMaterialReceipt(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = purchaseReceiptServiceservice.savePurchaseReceipt(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "查询全部供应商信息", tags = "查询全部供应商信息")
    @RequestMapping(value = "queryallsupplier", method = RequestMethod.GET)
    public WebResult queryAllSupperli() {
        try {
           var suppliers = supplierService.queryListAll();
           var companyList =orgSvc.queryAllProductionUnits();
            List<Map<String,Object>> data = new ArrayList<>();
            for (int i = 0; i < suppliers.size(); i++) {
                var supplier = suppliers.get(i);
                var item = supplier.toMap();
                var company = companyList.stream().filter(t->supplier.getOrganizationCode().contains(t.getSn())).findFirst();
                String cShortName = null;
                if(!company.isEmpty()) {
                   var cData = company.get();
                   if(Tl.isEmpty(cData.getShortName())){
                       cShortName = cData.getFullName();
                   }
                    else
                   {
                       cShortName = cData.getShortName();
                   }
                }
                if(!Tl.isEmpty(cShortName)) {
                    item.remove("supplierName");
                    item.put("supplierName",supplier.getSupplierName());
                }
                data.add(item);
            }
            return success("查询成功", data);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "根据适用类别查询仓库信息", tags = "根据适用类别查询仓库信息")
    @RequestMapping(value = "/queryrmWarehouse/{whUseType}", method = RequestMethod.GET)
    public WebResult queryRMWarehouse(@ApiParam(value = "查询仓库信息") @PathVariable() String whUseType) {
        try {
            return success("查询成功", irmWarehouseService.queryWarehoserAll(whUseType));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询原材入库单", notes = "根据查询原材入库单状态，条数，排序字段查询产品发货单")
    @RequestMapping(value = "/rawmaterialreceipt", method = RequestMethod.POST)
    public WebResult findProductShipOrderByState(@ApiParam(value = "发货单状态") @RequestParam short state, @ApiParam(value = "返回数据条数") @RequestParam int top, @ApiParam(value = "排序字段") @RequestParam String sort, @ApiParam(value = "查询条件") @RequestParam String key) {
        return success("查询成功", purchaseReceiptServiceservice.queryByState(state, top, sort, key));
    }

    @ApiOperation(value = "根据车牌号查询上次入库单据信息", notes = "根据车牌号查询上次入库单据信息")
    @RequestMapping(value = "/rawmaterialreceipt/{truckPlate}", method = RequestMethod.GET)
    public WebResult finByTruckPlate(@ApiParam(value = "发货单状态") @PathVariable String truckPlate) {
        try {
            return success("查询成功", purchaseReceiptServiceservice.queryByTruckPlate(truckPlate));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询所有已入库的车牌号", notes = "查询所有已入库的车牌号")
    @RequestMapping(value = "/rawmaterialreceiptruckall", method = RequestMethod.GET)
    public WebResult finByTruckAll() {
        try {
            return success("查询成功", purchaseReceiptServiceservice.queryAllTruckPlate());
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询所有原材分类信息", notes = "查询所有原材分类信息")
    @RequestMapping(value = "/queryallcate", method = RequestMethod.GET)
    public WebResult quesyAll() {
        try {
            return success("查询成功", RawMaterialCateservice.querySelect());
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询所有原材分类信息", notes = "查询所有原材分类信息")
    @RequestMapping(value = "/queryallInfo/{rmCateSN}", method = RequestMethod.GET)
    public WebResult quesyInfoBySn(@ApiParam(value = "原材种类编号") @PathVariable String rmCateSN) {
        try {
            return success("查询成功", RawMaterialInfoservice.queryListByRawCateSn(rmCateSN));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询所有原材分类信息", notes = "查询所有原材分类信息")
    @RequestMapping(value = "/querybyrmname/{rmName}", method = RequestMethod.GET)
    public WebResult quesyByRmName(@ApiParam(value = "原材种类编号") @PathVariable String rmName) {
        try {
            return success("查询成功", RawMaterialInfoservice.queryByRmName(rmName));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询供应商合同信息", notes = "查询供应商合同信息")
    @RequestMapping(value = "/queryPurchaseContractbysuppersn/{supplierSN}", method = RequestMethod.GET)
    public WebResult quesyPurchaseContractbySupplierSN(@ApiParam(value = "原材种类编号") @PathVariable String supplierSN) {
        try {
            return success("查询成功", purchaseContractServiceservice.querySupplierSn(supplierSN));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询所有原材分类信息", notes = "查询所有原材分类信息")
    @RequestMapping(value = "/quesyPurchaseContractProductBySn/{supplierSN}", method = RequestMethod.GET)
    public WebResult quesyPurchaseContractProductBySn(@ApiParam(value = "合同编号") @PathVariable String supplierSN) {
        try {
            return success("查询成功", purchaseContractProductService.queryListByContractSn(supplierSN).stream().filter(s-> Integer.parseInt(String.valueOf(s.get("state")))>=0));
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "根据供应商查询供应商下原材信息以及合同信息", notes = "根据供应商查询供应商下原材信息以及合同信息")
    @RequestMapping(value = "/queryPurchasebysuppliersn/{supplierSN}", method = RequestMethod.GET)
    public WebResult queryPurchasebySupplierSn(@ApiParam(value = "合同编号") @PathVariable String supplierSN) {
        try {
            Map<String,Object> result = new HashMap<>();
            var supper = purchaseContractServiceservice.querySupplierSn(supplierSN);
            var product = purchaseContractProductService.queryListByContractSn(supper.get(0).getSn());
            result.put("contract",supper.get(0));
            result.put("product",product);
            return success("查询成功", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "根据原材编号查询原材详细信息", notes = "根据原材编号查询原材详细信息")
    @RequestMapping(value = "/queryrawmatierbysn/{sn}", method = RequestMethod.GET)
    public WebResult queryRawmatierBysn(@ApiParam(value = "原材编号") @PathVariable String sn) {
        try {
            return success("查询成功", RawMaterialInfoservice.findBySn(sn));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "根据原材编号和合同编号查询供应商原材明细", notes = "根据原材编号和合同编号查询供应商原材明细")
    @RequestMapping(value = "/findByContract/{contractSn}/{rmSN}", method = RequestMethod.GET)
    public WebResult findByContract(@ApiParam(value = "合同编号") @PathVariable String contractSn, @ApiParam(value = "原材编号") @PathVariable String rmSN) {
        try {
            return success("查询成功", purchaseContractProductService.findByContract(contractSn, rmSN));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询所用仓库信息", notes = "查询所用仓库信息")
    @RequestMapping(value = "/querywarehouseall", method = RequestMethod.GET)
    public WebResult queryWareHouse() {
        try {
            return success("查询成功", irmWarehouseService.queryAll());
        } catch (Exception ee) {
            return error(ee);
        }
    }
}
